package com.example.schoolenterprisealliance.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.schoolenterprisealliance.MainActivity;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.adapter.TabFragmentPagerAdapter;
import com.example.schoolenterprisealliance.fragment.DynamicFragment;
import com.example.schoolenterprisealliance.fragment.HomePageFragment;
import com.example.schoolenterprisealliance.fragment.InternetFragment;
import com.example.schoolenterprisealliance.fragment.IntroduceFragment;
import com.example.schoolenterprisealliance.fragment.PolicyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainFragmentActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_pagehome,tv_introduce,tv_dynamic,tv_policy,tv_internet;
    private TextView tv_pagehome1,tv_introduce1,tv_dynamic1,tv_policy1,tv_internet1;
    private ViewPager myViewPager;
    private List<Fragment> list;
    private TabFragmentPagerAdapter adapter;
    private String colorString ="#A9A9A9";
    private EditText et_search;
    private ImageView iv_search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);
        InitView();
        // 设置菜单栏的点击事件
        tv_pagehome.setOnClickListener(this);
        tv_introduce.setOnClickListener(this);
        tv_dynamic.setOnClickListener(this);
        tv_policy.setOnClickListener(this);
        tv_internet.setOnClickListener(this);
        myViewPager.setOnPageChangeListener(new MyPagerChangeListener());
        adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list());
        myViewPager.setAdapter(adapter);
        myViewPager.setOffscreenPageLimit(5);
        myViewPager.setCurrentItem(0);  //初始化显示第一个页面
        tv_pagehome1.setVisibility(View.VISIBLE);
        tv_introduce1.setVisibility(View.INVISIBLE);
        tv_dynamic1.setVisibility(View.INVISIBLE);
        tv_policy1.setVisibility(View.INVISIBLE);
        tv_internet1.setVisibility(View.INVISIBLE);
        tv_pagehome.setTextColor(Color.BLACK);
        tv_introduce.setTextColor(Color.parseColor(colorString));
        tv_dynamic.setTextColor(Color.parseColor(colorString));
        tv_policy.setTextColor(Color.parseColor(colorString));
        tv_internet.setTextColor(Color.parseColor(colorString));
        search();
    }
    /**
     * 初始化控件
     */
    private void InitView() {
        tv_pagehome = (TextView) findViewById(R.id.tv_pagehome);
        tv_introduce = (TextView) findViewById(R.id.tv_introduce);
        tv_dynamic = (TextView) findViewById(R.id.tv_dynamic);
        tv_policy = (TextView) findViewById(R.id.tv_policy);
        tv_internet = (TextView) findViewById(R.id.tv_internet);
        myViewPager = (ViewPager) findViewById(R.id.myViewPager);
        tv_pagehome1 = (TextView) findViewById(R.id.tv_pagehome1);
        tv_introduce1 = (TextView) findViewById(R.id.tv_introduce1);
        tv_dynamic1 = (TextView) findViewById(R.id.tv_dynamic1);
        tv_policy1 = (TextView) findViewById(R.id.tv_policy1);
        tv_internet1 = (TextView) findViewById(R.id.tv_internet1);
        et_search = findViewById(R.id.et_search);
        iv_search = findViewById(R.id.iv_search);
    }
    //把Fragment添加到List集合里面
    private List<Fragment> list(){
        list = new ArrayList<>();
        list.add(new HomePageFragment());
        list.add(new IntroduceFragment());
        list.add(new DynamicFragment());
        list.add(new PolicyFragment());
        list.add(new InternetFragment());
        return  list;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pagehome:
                myViewPager.setCurrentItem(0);
                tv_pagehome1.setVisibility(View.VISIBLE);
                tv_introduce1.setVisibility(View.INVISIBLE);
                tv_dynamic1.setVisibility(View.INVISIBLE);
                tv_policy1.setVisibility(View.INVISIBLE);
                tv_internet1.setVisibility(View.INVISIBLE);
                tv_pagehome.setTextColor(Color.BLACK);
                tv_introduce.setTextColor(Color.parseColor(colorString));
                tv_dynamic.setTextColor(Color.parseColor(colorString));
                tv_policy.setTextColor(Color.parseColor(colorString));
                tv_internet.setTextColor(Color.parseColor(colorString));
                break;
            case R.id.tv_introduce:
                myViewPager.setCurrentItem(1);
                tv_pagehome1.setVisibility(View.INVISIBLE);
                tv_introduce1.setVisibility(View.VISIBLE);
                tv_dynamic1.setVisibility(View.INVISIBLE);
                tv_policy1.setVisibility(View.INVISIBLE);
                tv_internet1.setVisibility(View.INVISIBLE);
                tv_pagehome.setTextColor(Color.parseColor(colorString));
                tv_introduce.setTextColor(Color.BLACK);
                tv_dynamic.setTextColor(Color.parseColor(colorString));
                tv_policy.setTextColor(Color.parseColor(colorString));
                tv_internet.setTextColor(Color.parseColor(colorString));
                break;
            case R.id.tv_dynamic:
                myViewPager.setCurrentItem(2);
                tv_pagehome1.setVisibility(View.INVISIBLE);
                tv_introduce1.setVisibility(View.INVISIBLE);
                tv_dynamic1.setVisibility(View.VISIBLE);
                tv_policy1.setVisibility(View.INVISIBLE);
                tv_internet1.setVisibility(View.INVISIBLE);
                tv_pagehome.setTextColor(Color.parseColor(colorString));
                tv_introduce.setTextColor(Color.parseColor(colorString));
                tv_dynamic.setTextColor(Color.BLACK);
                tv_policy.setTextColor(Color.parseColor(colorString));
                tv_internet.setTextColor(Color.parseColor(colorString));
                break;
            case R.id.tv_policy:
                myViewPager.setCurrentItem(3);
                tv_pagehome1.setVisibility(View.INVISIBLE);
                tv_introduce1.setVisibility(View.INVISIBLE);
                tv_dynamic1.setVisibility(View.INVISIBLE);
                tv_policy1.setVisibility(View.VISIBLE);
                tv_internet1.setVisibility(View.INVISIBLE);
                tv_pagehome.setTextColor(Color.parseColor(colorString));
                tv_introduce.setTextColor(Color.parseColor(colorString));
                tv_dynamic.setTextColor(Color.parseColor(colorString));
                tv_policy.setTextColor(Color.BLACK);
                tv_internet.setTextColor(Color.parseColor(colorString));
                break;
            case R.id.tv_internet:
                myViewPager.setCurrentItem(4);
                tv_pagehome1.setVisibility(View.INVISIBLE);
                tv_introduce1.setVisibility(View.INVISIBLE);
                tv_dynamic1.setVisibility(View.INVISIBLE);
                tv_policy1.setVisibility(View.INVISIBLE);
                tv_internet1.setVisibility(View.VISIBLE);
                tv_pagehome.setTextColor(Color.parseColor(colorString));
                tv_introduce.setTextColor(Color.parseColor(colorString));
                tv_dynamic.setTextColor(Color.parseColor(colorString));
                tv_policy.setTextColor(Color.parseColor(colorString));
                tv_internet.setTextColor(Color.BLACK);
                break;
        }
    }
    /**
     * 设置一个ViewPager的监听事件，当左右滑动ViewPager时菜单栏被选中状态跟着改变
     *
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    tv_pagehome1.setVisibility(View.VISIBLE);
                    tv_introduce1.setVisibility(View.INVISIBLE);
                    tv_dynamic1.setVisibility(View.INVISIBLE);
                    tv_policy1.setVisibility(View.INVISIBLE);
                    tv_internet1.setVisibility(View.INVISIBLE);
                    tv_pagehome.setTextColor(Color.BLACK);
                    tv_introduce.setTextColor(Color.parseColor(colorString));
                    tv_dynamic.setTextColor(Color.parseColor(colorString));
                    tv_policy.setTextColor(Color.parseColor(colorString));
                    tv_internet.setTextColor(Color.parseColor(colorString));
                    break;
                case 1:
                    tv_pagehome1.setVisibility(View.INVISIBLE);
                    tv_introduce1.setVisibility(View.VISIBLE);
                    tv_dynamic1.setVisibility(View.INVISIBLE);
                    tv_policy1.setVisibility(View.INVISIBLE);
                    tv_internet1.setVisibility(View.INVISIBLE);
                    tv_pagehome.setTextColor(Color.parseColor(colorString));
                    tv_introduce.setTextColor(Color.BLACK);
                    tv_dynamic.setTextColor(Color.parseColor(colorString));
                    tv_policy.setTextColor(Color.parseColor(colorString));
                    tv_internet.setTextColor(Color.parseColor(colorString));
                    break;
                case 2:
                    tv_pagehome1.setVisibility(View.INVISIBLE);
                    tv_introduce1.setVisibility(View.INVISIBLE);
                    tv_dynamic1.setVisibility(View.VISIBLE);
                    tv_policy1.setVisibility(View.INVISIBLE);
                    tv_internet1.setVisibility(View.INVISIBLE);
                    tv_pagehome.setTextColor(Color.parseColor(colorString));
                    tv_introduce.setTextColor(Color.parseColor(colorString));
                    tv_dynamic.setTextColor(Color.BLACK);
                    tv_policy.setTextColor(Color.parseColor(colorString));
                    tv_internet.setTextColor(Color.parseColor(colorString));
                    break;
                case 3:
                    tv_pagehome1.setVisibility(View.INVISIBLE);
                    tv_introduce1.setVisibility(View.INVISIBLE);
                    tv_dynamic1.setVisibility(View.INVISIBLE);
                    tv_policy1.setVisibility(View.VISIBLE);
                    tv_internet1.setVisibility(View.INVISIBLE);
                    tv_pagehome.setTextColor(Color.parseColor(colorString));
                    tv_introduce.setTextColor(Color.parseColor(colorString));
                    tv_dynamic.setTextColor(Color.parseColor(colorString));
                    tv_policy.setTextColor(Color.BLACK);
                    tv_internet.setTextColor(Color.parseColor(colorString));
                    break;
                case 4:
                    tv_pagehome1.setVisibility(View.INVISIBLE);
                    tv_introduce1.setVisibility(View.INVISIBLE);
                    tv_dynamic1.setVisibility(View.INVISIBLE);
                    tv_policy1.setVisibility(View.INVISIBLE);
                    tv_internet1.setVisibility(View.VISIBLE);
                    tv_pagehome.setTextColor(Color.parseColor(colorString));
                    tv_introduce.setTextColor(Color.parseColor(colorString));
                    tv_dynamic.setTextColor(Color.parseColor(colorString));
                    tv_policy.setTextColor(Color.parseColor(colorString));
                    tv_internet.setTextColor(Color.BLACK);
                    break;
            }
        }
    }
    //点击跳转
    private void search(){
        et_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainFragmentActivity.this, SearchHotActivity.class);
                startActivity(intent);
            }
        });

        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainFragmentActivity.this,SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }


}
